<template>
  <div class="dashboard">
    <h1>农产品溯源系统</h1>
    
    <div class="card dashboard-summary">
      <h2>系统概览</h2>
      <div v-if="loading" class="loading-indicator">
        数据加载中...
      </div>
      <div v-else class="summary-grid">
        <div class="summary-item">
          <div class="value">{{ productCount }}</div>
          <div class="label">活跃产品数量</div>
        </div>
        <div class="summary-item">
          <div class="value">{{ ownerAddress }}</div>
          <div class="label">合约所有者</div>
        </div>
      </div>
    </div>
    
    <div class="card">
      <h2>系统功能</h2>
      <div class="features-grid">
        <div class="feature-item" @click="$router.push('/products')">
          <i class="feature-icon">📋</i>
          <h3>产品列表</h3>
          <p>查看系统中所有的产品信息和溯源数据</p>
        </div>
        <div class="feature-item" @click="$router.push('/add-product')">
          <i class="feature-icon">➕</i>
          <h3>添加产品</h3>
          <p>向系统中添加新的农产品</p>
        </div>
      </div>
    </div>
    
    <div class="card">
      <h2>系统介绍</h2>
      <p>本系统基于区块链技术，为农产品提供端到端的可追溯性，确保农产品从生产到销售的每个环节都能被记录和验证。</p>
      <p>系统主要功能包括：</p>
      <ul>
        <li>产品信息记录：记录产品名称、生产商、生产日期、原产地等信息</li>
        <li>质检记录管理：添加和查看产品的质检信息</li>
        <li>产品所有权转移：记录产品在供应链中的所有权变更</li>
        <li>数据完整性验证：验证产品信息是否被篡改</li>
      </ul>
    </div>
  </div>
</template>

<script>
import { systemApi, productApi } from '@/services/api'

export default {
  name: 'Dashboard',
  data() {
    return {
      ownerAddress: '暂无数据',
      productCount: 0,
      loading: false,
      error: null
    }
  },
  mounted() {
    this.fetchDashboardData()
  },
  methods: {
    async fetchDashboardData() {
      this.loading = true
      try {
        // 获取合约所有者
        const ownerResponse = await systemApi.getOwner()
        if (ownerResponse && ownerResponse.data) {
          this.ownerAddress = ownerResponse.data || '暂无数据'
        }
        
        // 获取产品列表计数
        const productsResponse = await productApi.getProductsList()
        if (productsResponse && productsResponse.data) {
          this.productCount = Array.isArray(productsResponse.data) ? productsResponse.data.length : 0
        } else {
          this.productCount = 0
        }
      } catch (error) {
        console.error('获取数据失败', error)
        this.error = '获取数据失败，请检查网络连接或后端服务是否正常运行'
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>
.dashboard-summary {
  margin-bottom: 2rem;
}

.loading-indicator {
  text-align: center;
  padding: 1rem;
  font-style: italic;
  color: #5f9ea0; /* 浅蓝色文字 */
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 1rem;
}

.summary-item {
  text-align: center;
  padding: 1rem;
  background-color: #f0f8ff; /* 浅蓝色背景 */
  border-radius: 8px;
  border: 1px solid #b0e0e6; /* 浅蓝色边框 */
}

.summary-item .value {
  font-size: 1.25rem;
  font-weight: bold;
  margin-bottom: 0.5rem;
  word-break: break-all;
  color: #6aa7d9; /* 浅绿色文字 */
}

.summary-item .label {
  font-size: 0.9rem;
  color: #5f9ea0; /* 浅蓝色文字 */
}

.features-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 1.5rem;
  margin-top: 1rem;
}

.feature-item {
  background-color: #f0f8ff; /* 浅蓝色背景 */
  border-radius: 8px;
  padding: 1.5rem;
  text-align: center;
  transition: transform 0.3s, box-shadow 0.3s;
  cursor: pointer;
  border: 1px solid #b0e0e6; /* 浅蓝色边框 */
}

.feature-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(176, 224, 230, 0.4); /* 浅蓝色阴影 */
}

.feature-icon {
  font-size: 2.5rem;
  margin-bottom: 1rem;
  display: inline-block;
  color: #20b2aa; /* 浅绿色文字 */
}

.feature-item h3 {
  margin-bottom: 0.5rem;
  color: #20b2aa; /* 浅绿色文字 */
}

.feature-item p {
  color: #5f9ea0; /* 浅蓝色文字 */
}

ul {
  padding-left: 1.5rem;
  margin-top: 0.5rem;
}

li {
  margin-bottom: 0.5rem;
  color: #5f9ea0; /* 浅蓝色文字 */
}

.card {
  background-color: #fff;
  border-radius: 8px;
  padding: 1.5rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 1px solid #b0e0e6; /* 浅蓝色边框 */
  margin-bottom: 1.5rem;
}

h1, h2 {
  color: #1d76b5; /* 浅绿色文字 */
}

p {
  color: #5f9ea0; /* 浅蓝色文字 */
}
</style> 